library(dplyr)

external <- read.csv("UCDP External Actor.csv")
negotiations <- read.csv("PNCC.csv")
governance <- read.csv("Rebel Governance.csv")
actor <- read.csv("UCDP-Actor.csv")

# rename column names
negotiations <- rename(negotiations, actor_id = ActorId_rebel)
governance <- rename(governance, actor_id = actorid)

names(negotiations) <- tolower(names(negotiations))
names(governance) <- tolower(names(governance))


# create fragmented variable

negotiations <- negotiations %>%
  select(actor_id, year, negotiation, minyearacd, splinter, year, actoridprev)

for(i in 1:length(negotiations$actor_id))
{
  negotiations$timeofsplinter[i] <- ifelse(negotiations$actor_id[i] %in% negotiations$actoridprev,
                                           negotiations$minyearacd[max(which(negotiations$actoridprev == negotiations$actor_id[i]))], 0)
}

for(j in 1:length(negotiations$actor_id))
{
  negotiations$fragmented[j] <- ifelse(negotiations$year[j] >= negotiations$timeofsplinter[j],
                                       1, 0)
}

# once a group negotiates, change the variable to 1 for future years

for(x in 1:length(negotiations$actor_id))
{
  negotiations$negotiation[x] <- ifelse((negotiations$negotiation[x] == 1 | negotiations$negotiation[x] == 2), 1, 0)
}

for(y in 2:length(negotiations$actor_id))
{
  negotiations$negotiation[y] <- ifelse(negotiations$negotiation[y] == 1 |
                                          (negotiations$actor_id[y] == negotiations$actor_id[y-1] &
                                             (negotiations$negotiation[y-1] == 1 |
                                                negotiations$negotiation[y-1])), 1, 0)
}


# new column that evaluates fragmented and negotiations
negotiations$status <- 0

# negotiated but not fragmented
negotiations$status[negotiations$negotiation == 1 & negotiations$fragmented == 0] <- 1

# negotiated and fragmented
negotiations$status[negotiations$negotiation == 1 & negotiations$fragmented == 1] <- 2

# not negotiated but fragmented
negotiations$status[negotiations$negotiation == 0 & negotiations$fragmented == 1] <- 3

# not negotiated and not fragmented
negotiations$status[negotiations$negotiation == 0 & negotiations$fragmented == 0] <- 4

# creates binary if spoils or not

negotiations$spoiler[1] <- 0

for(w in 2:length(negotiations$actor_id))
{
  negotiations$spoiler[w] <- ifelse(negotiations$status[w] == 2 &
                                (negotiations$actor_id[w] == negotiations$actor_id[w-1] &
                                (negotiations$status[w-1] == 1) | (negotiations$status[w-1] == 4)), 1, 0)
}


# only keep if before or during spoiler, or did not spoil, and did negotiate

for(z in 1:length(negotiations$actor_id))
{
  negotiations$delete[z] <- ifelse(negotiations$year[z] <= negotiations$timeofsplinter[z] | 
                                     negotiations$timeofsplinter[z] == 0, 0, 1)
}

negotiations <- negotiations %>%
  filter(delete == 0) %>%
  filter(negotiation == 1)

# aggregate and clean up datasets

negotiations <- negotiations %>%
  select(actor_id, year, spoiler, timeofsplinter)

governance <- governance %>%
  group_by(actor_id, year) %>%
  summarise(taxation = taxation,
            illegal_network = illegalnet,
            negotiate_resources = negresource)

external <- external %>%
  group_by(actor_id, year) %>%
  summarise(ext_troop_support = max(ext_x),
            ext_troop_presence = max(ext_p),
            ext_access_to_infrastructure = max(ext_y),
            ext_logistics = max(ext_m),
            ext_weapons = max(ext_w),
            ext_training = max(ext_t),
            ext_funding = max(ext_f),
            ext_intelligence = max(ext_i),
            ext_territory = max(ext_l))


# merge datasets 

financing <- left_join(negotiations, external, by=c("actor_id", "year"))
financing <- left_join(financing, governance, by=c("actor_id", "year"))

financing <- financing %>%
  group_by(actor_id, year) %>%
  summarise(spoiler = max(spoiler),
            timeofsplinter = max(timeofsplinter),
            taxation = max(taxation),
            illegal_network = max(illegal_network),
            negotiate_resources = max(negotiate_resources),
            
            ext_support = max(ext_training, ext_troop_support, ext_weapons,
                              ext_troop_presence, ext_logistics, ext_intelligence,
                              ext_territory, ext_access_to_infrastructure),
            ext_funding = max(ext_funding),
            )

# look for more spoilers

actor <- actor %>%
  group_by(ActorIdPrev) %>%
  filter(ActorIdPrev %in% financing$actor_id) %>%
  select(NameData, ActorIdPrev, NamePrev) %>%
  rename(splinter_group_name = NameData,
         parent_group_id = ActorIdPrev,
         parent_group_name = NamePrev)

temporary_financing <- financing %>%
  group_by(actor_id) %>%
  summarize(spoiler = max(spoiler)) %>%
  select(actor_id, spoiler)

actor$needs_research <- 1

for(e in 1:length(actor$parent_group_id))
{
  actor$needs_research[e] <- ifelse(temporary_financing$spoiler[which(actor$parent_group_id[e] == 
                                                                        temporary_financing$actor_id)] == 1,
                                    0, 1)
    
}

actor <- actor %>%
  filter(needs_research == 1) %>%
  select(parent_group_id, parent_group_name, splinter_group_name)

# adding spoilers using outside research
financing$spoiler[which(financing$actor_id == 223 & financing$year == 2001)] <- 1
financing$spoiler[which(financing$actor_id == 275 & financing$year == 1977)] <- 1
financing$spoiler[which(financing$actor_id == 305 & financing$year == 1988)] <- 1
financing$spoiler[which(financing$actor_id == 320 & financing$year == 1989)] <- 1
financing$spoiler[which(financing$actor_id == 320 & financing$year == 2004)] <- 1
financing$spoiler[which(financing$actor_id == 424 & financing$year == 1999)] <- 1
financing$spoiler[which(financing$actor_id == 521 & financing$year == 1992)] <- 1
financing$spoiler[which(financing$actor_id == 530 & financing$year == 1994)] <- 1
financing$spoiler[which(financing$actor_id == 533 & financing$year == 1999)] <- 1
financing$spoiler[which(financing$actor_id == 558 & financing$year == 2007)] <- 1
financing$spoiler[which(financing$actor_id == 529 & financing$year == 1991)] <- 1
financing$spoiler[which(financing$actor_id == 466 & financing$year == 1992)] <- 1
financing$spoiler[which(financing$actor_id == 466 & financing$year == 1997)] <- 1
financing$spoiler[which(financing$actor_id == 432 & financing$year == 2007)] <- 1
financing$spoiler[which(financing$actor_id == 300 & financing$year == 1996)] <- 1
financing$spoiler[which(financing$actor_id == 354 & financing$year == 1994)] <- 1
financing$spoiler[which(financing$actor_id == 5659 & financing$year == 2004)] <- 1
financing$spoiler[which(financing$actor_id == 503 & financing$year == 1995)] <- 1
financing$spoiler[which(financing$actor_id == 513 & financing$year == 1993)] <- 1
financing$spoiler[which(financing$actor_id == 356 & financing$year == 2013)] <- 1
financing$spoiler[which(financing$actor_id == 554 & financing$year == 2013)] <- 1
financing$spoiler[which(financing$actor_id == 1160 & financing$year == 2013)] <- 1
financing$spoiler[which(financing$actor_id == 303 & financing$year == 2013)] <- 1
financing$spoiler[which(financing$actor_id == 1158 & financing$year == 2013)] <- 1
financing$spoiler[which(financing$actor_id == 746 & financing$year == 1991)] <- 1
financing$spoiler[which(financing$actor_id == 541 & financing$year == 2010)] <- 1

financing <- financing[!(financing$actor_id == 223 & financing$year > 2001), ]
financing <- financing[!(financing$actor_id == 275 & financing$year > 1977), ]
financing <- financing[!(financing$actor_id == 320 & financing$year > 2004), ]
financing <- financing[!(financing$actor_id == 424 & financing$year > 1999), ]
financing <- financing[!(financing$actor_id == 558 & financing$year > 2007), ]
financing <- financing[!(financing$actor_id == 529 & financing$year > 1991), ]
financing <- financing[!(financing$actor_id == 466 & financing$year > 1997), ]
financing <- financing[!(financing$actor_id == 432 & financing$year > 2007), ]
financing <- financing[!(financing$actor_id == 354 & financing$year > 1994), ]
financing <- financing[!(financing$actor_id == 5659 & financing$year > 2004), ]
financing <- financing[!(financing$actor_id == 503 & financing$year > 1995), ]
financing <- financing[!(financing$actor_id == 513 & financing$year > 1993), ]
financing <- financing[!(financing$actor_id == 541 & financing$year > 2010), ]
financing <- financing[!(financing$actor_id == 743 & financing$year > 2013), ]

# account for time dependence

financing$time_at_risk <- 0
financing$time_at_risk[1] <- 1
financing$time_at_risk_squared <- 0
financing$time_at_risk_cubed <- 0

for(f in 2:length(financing$actor_id))
{
  financing$time_at_risk[f] <- ifelse((financing$actor_id[f] == financing$actor_id[f-1]), 
                                    (financing$time_at_risk[f-1] + 1), 1)
}

for(h in 1:length(financing$actor_id))
{
  financing$time_at_risk_squared[h] = financing$time_at_risk[h] ^ 2
  financing$time_at_risk_cubed[h] = financing$time_at_risk[h] ^ 3
}

# delete values with N/A

financing$delete <- 0
for(m in 1:length(financing$actor_id))
{
  financing$delete[m] <- ifelse(is.na(financing$taxation[m]) | is.na(financing$illegal_network[m]) |
                                      is.na(financing$negotiate_resources[m]) | 
                                      is.na(financing$ext_funding[m]) |
                                      is.na(financing$ext_support[m]), 1, 0)
}

financing <- financing %>%
  filter(delete == 0) %>%
  select(-c(delete))

# create summary statistics
write.csv(financing, file = "financing.csv")